def datetime_to_string(dt):
    return dt.strftime("%Y-%m-%d %H:%M:%S")


def get_ts_code():
    import pymysql
    from tushare_api import fetch_data_pro_bar
    from datetime import timedelta
    from datetime import datetime

    db = pymysql.connect("127.0.0.1", "root", "root", "gupiao")
    # 使用 cursor() 方法创建一个游标对象 cursor
    cursor = db.cursor()

    sql = "select ts_code, max(trade_time) max_time from pro_bar_index group by ts_code"

    cursor.execute(sql)

    ts_codes = cursor.fetchall()
    for item in ts_codes:
        ts_code = item[0]
        max_time = item[1]
        nearly_time = datetime(datetime.today().year, datetime.today().month, datetime.today().day)
        print(ts_code)
        print(type(max_time))
        start_time_str = datetime_to_string(max_time + timedelta(minutes=1))
        end_time_str = datetime_to_string(nearly_time + timedelta(days=1))
        print(ts_code + start_time_str + end_time_str)
        fetch_data_pro_bar(ts_code, start_time_str, end_time_str, "index", "5min")


# get_ts_code()
